import { defineStore } from "pinia"; // 引入pinia
import storage from '@/utils/storage'

type UserInfo = {
  emailMask: string;
  lastLoginSuccessTime: string;
  opUserNo: string;
  subscriptionNo: string;
  subscriptionAuditStatus: string;
  phoneNoMask: string;
  relation: object;
  // 当前选择登陆的商户
  regInfo: object;
}

export const useUserStateStore = defineStore("userInfo", {
  state: () => {
    return {
      token: '',
      emailMask: '',
      opUserNo: '',
      subscriptionNo: '',
      subscriptionAuditStatus: '',
      phoneNoMask: '',
      userName: '',
      relation: {},
      regInfo: {},
    };
  },
  actions: {
    getToken() {
       storage.getToken()
       return this.token
    },
    setToken(token: string) {
      token = 'Bearer ' + token
      this.token = token
      storage.setToken(token)
    },
    removeToken() {
      this.token = ''
      storage.removeToken()
      storage.removeUserInfo()
    },
    setUserInfo(users: UserInfo) {
      this.emailMask = users.emailMask
      this.opUserNo = users.opUserNo
      this.phoneNoMask = users.phoneNoMask
      this.userName = users.phoneNoMask || users.emailMask || users.opUserNo
      this.subscriptionNo = users.subscriptionNo
      this.subscriptionAuditStatus = users.subscriptionAuditStatus
    },
    setRelation(relation: any) {
      this.relation = relation
    },
    setRegInfo(regInfo: any) {
      this.regInfo = regInfo
    },
  },
  persist: process.client && { // 仅在客户端使用
    storage: localStorage, // localStorage 本地存储，可替换sessionStorage
  },
});